package com.crm.action;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.crm.dao.PaperDao;
import com.crm.dao.QuestionDao;
import com.crm.model.PageBean;
import com.crm.model.business.Paper;
import com.crm.model.business.Question;
import com.crm.util.PageUtil;
import com.crm.util.ResponseUtil;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class QuestionAction extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest request;
    private QuestionDao questionDao = new QuestionDao();
    private PaperDao paperDao = new PaperDao();
    private String mainPage;
    private String page;
    private List<Question> questionList;
    private List<Paper> paperList;
    private Question question;
    private int total;
    private String pageCode;
    private String id;
    private String title;
    private int pageSize = 5;

    public String list() {
        if (ObjectUtils.isEmpty(page)) {
            page = "1";
        }
        PageBean pageBean = new PageBean(Integer.parseInt(page), pageSize);
        if(ObjectUtils.allNotNull(question)){
            questionList = questionDao.list(question, pageBean);
            total = questionDao.count(question);
        }else {
            questionList = questionDao.list(pageBean);
            total = questionDao.count();
        }
        pageCode = PageUtil.genPagination(request.getContextPath() + "/questionAction!list", total, Integer.parseInt(page), pageSize);
        mainPage = "business/question/questionList.jsp";
        return SUCCESS;
    }

    public String getQuestionById() {
        question = questionDao.getQuestionById(id);
        mainPage = "business/question/questionShow.jsp";
        return SUCCESS;
    }


    public String preSave(){
        paperList = paperDao.list();
        if (StringUtils.isNotEmpty(id)) {
            question = questionDao.getQuestionById(id);
            title = "修改试卷信息";
        } else {
            title = "添加试卷信息";
        }
        mainPage = "business/question/questionSave.jsp";
        return SUCCESS;
    }

    public String save(){
        if (StringUtils.isNotEmpty(id)) {
            question.setId(Integer.parseInt(id));
        }
        questionDao.save(question);
        return "save";
    }

    public void delete(){
        question = questionDao.getQuestionById(id);
        questionDao.delete(question);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("success", true);
        ResponseUtil.write(JSON.toJSONString(resultMap), ServletActionContext.getResponse());
    }










    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question s_question) {
        this.question = s_question;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public void setPaperList(List<Paper> paperList) {
        this.paperList = paperList;
    }

    public List<Paper> getPaperList() {
        return paperList;
    }

    public void setQuestionList(List<Question> questionList) {
        this.questionList = questionList;
    }

    public List<Question> getQuestionList() {
        return questionList;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public String getPageCode() {
        return pageCode;
    }

    public void setPageCode(String pageCode) {
        this.pageCode = pageCode;
    }

    public String getMainPage() {
        return mainPage;
    }

    public void setMainPage(String mainPage) {
        this.mainPage = mainPage;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
}
